﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text;
using System.Net.Sockets;
using Kugar.Core.BaseStruct;
using Kugar.Core.ExtMethod;


namespace Kugar.Core.Communications.Transfer
{
    public class SocketAsyncEventArgsEx:SocketAsyncEventArgs,IRecyclable
    {
        private SocketAsyncEventArgsPool _pool;

        public SocketAsyncEventArgsEx(SocketAsyncEventArgsPool pool)
        {
            _pool = pool;
        }

        #region Implementation of IRecyclable

        public IRecyclablePool<IRecyclable> Pool
        {
            get
            {
                return (IRecyclablePool<IRecyclable>) _pool;
            }
            set { }
        }

        /// <summary>
        ///     当对象真正释放的时候，回收器会调用该函数
        /// </summary>
        public void DisposeObject()
        {
            base.Dispose();
        }

        #endregion

        public new void Dispose()
        {
            //base.Completed -= this.Completed;

            _pool.RecycleObject(this);

        }

    }

    public class SocketAsyncEventArgsPool:RecyclablePool<SocketAsyncEventArgsEx>
    {
        public SocketAsyncEventArgsPool(int maxLength,int minLength):base(maxLength,minLength)
        {
            base.Init();
        }

        #region Overrides of RecyclablePool<SocketAsyncEventArgsEx>

        /// <summary>
        ///     构建一个新的对象,该函数必须由继承的类实现
        /// </summary>
        /// <returns></returns>
        protected override SocketAsyncEventArgsEx CreateRecyclableObject()
        {
            var temp = new SocketAsyncEventArgsEx(this);

            return temp;
        }

        #endregion
    }
}
